/* Copyright(C): Leaf Johnson 2009,2010

    This file is part of leaf-lib.

    leaf-lib is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    leaf-lib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with leaf-lib.  If not, see <http://www.gnu.org/licenses/>.


 * 
 * 
 */


#include <devices/sysclk.h>
#include <leaf/osiface.h>
static uint32_t jaffies = 0;
static unsigned hz = 250;
static uint8_t tcnt = 255;
void sysclk_init( uint8_t tccr, unsigned div, unsigned tickRate )
{
	uint8_t sr = SREG;
	cli();
	hz = tickRate;
	tcnt = TICKRATE_TCNT( tickRate, div );
	/* 根据指定的时钟节拍来选择合适的分频和TCNT */
	TCCR0 = 0;
	TCNT0 = tcnt;
	TCCR0 = tccr;
	set_bit( TIMSK, TOIE0 );
	SREG = sr;
}

inline unsigned getHz( void )
{
	return hz;
}

inline uint32_t get_jaffies( void )
{
	return jaffies;
}

uint32_t get_running_ms( void )
{
	uint32_t msecs = jaffies;
	msecs = TICKS_MS( msecs );
	return msecs;
}

DEF_OSISR( TIMER0_OVF_vect )
{
	enter_kernel();
	TCNT0 = tcnt;
	time_tick();
	enter_task();
}

